<?php

namespace Admin\Controller;

use Think\Controller;

class UserController extends Controller {

    /**
     * 显示所有用户
     */
    public function index($page=0){
        obtain();
        $Model = M('user');
        $count = $Model->count();
        $pagenum = ($page < 2 || (($page-1)*10)>$count)?0:($page-1)*10;
        $college = $Model->limit($pagenum,10)->order('userAccount asc')->select();
        $this->assign('zy', $name);
        $this->assign('list', $college);
        $page = getPageHtml($page,$count,__ROOT__."/Admin/User/Index");
        $this->assign('page',$page);
        $this->display('index');
        
    }
    /*
     * 批量导入用户
     */
    public function add() {
        obtain();
        if (empty($_FILES)) {
            $this->display();
            exit;
        }
        ini_set('memory_limit', '1024M');
        $config = array(
            'exts' => array('xlsx', 'xls'),
            'maxSize' => 3145728000,
            'rootPath' => "./Data/",
            'savePath' => 'Uploads/',
            'subName' => array('date', 'Ymd'),
        );
        $upload = new \Think\Upload($config);
        if (!$info = $upload->upload()) {
            $this->error($upload->getError());
        }
        import("Org.Util.PHPExcel");
        import("Org.Util.PHPExcel.IOFactory");
        $file_name = $upload->rootPath . $info['photo']['savepath'] . $info['photo']['savename'];
        $extension = strtolower(pathinfo($file_name, PATHINFO_EXTENSION)); //判断导入表格后缀格式
        if ($extension == 'xlsx') {
            $objReader = \PHPExcel_IOFactory::createReader('Excel2007');
            $objPHPExcel = $objReader->load($file_name, $encode = 'utf-8');
        } else if ($extension == 'xls') {
            $objReader = \PHPExcel_IOFactory::createReader('Excel5');
            $objPHPExcel = $objReader->load($file_name, $encode = 'utf-8');
        }
        $sheet = $objPHPExcel->getSheet(0);
        $highestRow = $sheet->getHighestRow(); //取得总行数
        $highestColumn = $sheet->getHighestColumn(); //取得总列数

        for ($i = 2; $i <= $highestRow; $i++) {
            //看这里看这里,前面小写的a是表中的字段名，后面的大写A是excel中位置
            if (empty((String) $objPHPExcel->getActiveSheet()->getCell("B" . $i)->getValue())) {
                continue;
            }
            $data[] = array(
                'userAccount' => (String) $objPHPExcel->getActiveSheet()->getCell("A" . $i)->getValue(),
                'name' => (String) $objPHPExcel->getActiveSheet()->getCell("B" . $i)->getValue(),
                'password' => encrypt((String) $objPHPExcel->getActiveSheet()->getCell("A" . $i)->getValue()),
                'sex' => (String) $objPHPExcel->getActiveSheet()->getCell("C" . $i)->getValue(),
                'department' => (String) $objPHPExcel->getActiveSheet()->getCell("D" . $i)->getValue(),
            );
        }
        if (M('User')->addAll($data)) {
            $this->success('导入成功!');
        } else {
            $this->error('失败');
        }
    }
    public function addone() {
        obtain();
        $_POST['password'] = encrypt($_POST['userAccount']);
        $teacher = M('user');
        $teacher->create();
        if ($teacher->where("userAccount='%s'", I('post.userAccount'))->count()) {
            $this->error('用户已存在');
        } else {
            if ($teacher->add()) {
                $this->success('导入成功!');
            } else {
                $this->error('失败');
            }
        }
    }
    public function addoneView() {
        $this->display('addone');
    }
    /**
     * 初始化密码
     * [teacher description]
     * @return [type] [description]
     */
    public function initialize($number){
    	obtain();
    	$t['pwd'] = encrypt($number);
       	M('user')->where ('userAccount=\'%s\'',$number)->save($t);
       	$this->success('重置成功！密码为：'.$number);
    }
    /**
     * 初始化教师密码
     * [teacher description]
     * @return [type] [description]
     */
    public function admin(){
    	obtain();
        empty($_POST)?exit:1;
        $tea = filter_input(INPUT_POST, 'tea');
        $val = filter_input(INPUT_POST, 'val');
        ($val != 0 && $val != 1)?exit:1;
        if(M('user')->where(array('userAccount'=>$tea))->save(array('identity'=>$val))){
            $this->ajaxReturn(array('val'=>1));
        }else{
            $this->ajaxReturn(array('val'=>0));
        }
    }
    /**
     * 
     * @param type $number
     */
     public function deleteUser($number){
    	obtain();
       	if (M('user')->where ('userAccount=\'%s\'',$number)->delete()){
       		$this->success('成功删除该账号！');
       	} else {
       		$this->error('未能删除，请检查原因！');
       	}
    }
}
